//密码MD5加密
function md5Password(form){
	var oPassword=form["password"].value;
	form["password"].value=hex_md5(oPassword);
}

$(function() {
	$('input[placeholder]').placeholder({
		  useNative: false, 
		  hideOnFocus: false
	});
	
	//数据检查
	$("#loginform").validate({
		submitHandler: function(form) {
			$("#loginform .meg-tip").hide();
			var uname = form["loginName"].value
			md5Password(form);
		    $(form).vkoAjaxSubmitP({
		    	success:function(data){
		    		$.cookie("vkouser", uname, {path: '/', domain: 'vko.cn'})
		    		window.location.href=data;
		    	},
		    	error:function(data){
		    		$("#loginform .meg-tip").html(data.message).show();
		    		$("#password").val("").next("div").show();
		    	}
		    })
		},
		rules:{
			loginName:{required:true},
			password:{required:true,minlength:6}
		},
		messages:{
			loginName:{required:"请输入你的常用邮箱"},
			password:{required:"请输入密码，最少6位",minlength:"密码太短了，最少6位"}
		}
	});
	$("#loginform input").focus(function(){
		$("#loginform .meg-tip").hide();
	});
	//登陆邮箱提示
	if($.cookie("vkouser")!=null){
		$("#email").val($.cookie("vkouser"));
		$("#email").parent("span").next("div").hide();
		$("#email").parent().append("<span class='login_del'></span>");
		$(".login_del").fadeIn();
	}
	$(".login_del").click(function(){
		$("#email").val("");
		$("#email").focus();
		$("#password").val("");
		$("#password").siblings("div").show();
		$.cookie("vkouser", null, {path: '/', domain: 'vko.cn'});
		$(this).fadeOut();
	});
	$("#email").mailAutoComplete({
		boxClass: "out_box", //外部box样式
		listClass: "list_box", //默认的列表样式
		focusClass: "focus_box", //列表选样式中
		markCalss: "mark_box", //高亮样式
		autoClass: false,
		textHint: false, //提示文字自动隐藏
		hintText: ""
	});
	setInterval(function(){
		if($("#password").val()!=""){
			$("#password").siblings("div").hide();
		}
		if($("#email").val()!=""){
			$("#email").parent().siblings("div").hide();
		}
	},100);
	
	//顶部更多
	$(".btn-more").click(function(){
		$(".menu-more").slideToggle("fast");
	});
	$("#wrap").mouseover(function(){
		if($(".menu-more:hidden")){
			$(".menu-more").slideUp("fast");
		}
	});
	
	//微课活动轮换图
	$('.imgscroll').vkoScroll({
		easing: "easeInOutExpo",        
		autoPlay: true,                
		delay: 3000,                   
		startStopped: false,
		animationTime: 600, 
		hashTags: true,  
		buildNavigation: true,   
		pauseOnHover: true
	});
	$(".imgscroll li img").live('click',function(){
		showMedia($(this).attr("hd"),$(this).attr("hd"),$(this).attr("title"),"true");
	});

	//微课名师
	$(".vkoteacher .fitimg").hover(function () {
		if($(this).hasClass("curimg")){
			return false;
		}else{
			$(".blueline").css({"display":"block"});
			$(".blueline").animate({"left":$(this).position().left-3, "top":$(this).position().top-3}, 100);
			$(this).fadeTo("0", 1);
		}		
	}, function () {
		if($(this).hasClass("curimg")){
			return false;
		}else{
			$(".blueline").css("display", "none");
			$(this).fadeTo("0", 0.5);
		}
	}).click(function () {
		clearInterval(aotoShow)
		imgClick($(this));
	});
	function imgClick(img){
		$this = img;
		if($this.hasClass("curimg")){
			return false;
		}else{
			$(".vkoteacher").find(".curimg").removeClass("curimg").fadeTo("0", 0.5);
			$this.addClass("curimg");
			if ($this.parent().attr("class") != "select") {
				$(".redline").animate({"left":$this.position().left-3, "top":$this.position().top-3}, 100);
			}
			$(".blueline").css("display", "none");
			$(".vkoteacher dt").html($this.siblings(".infoload").html());
		}
	}
	$(".vkoteacher .fitimg:first").click();
	$(".vkoteacher dt .btn-show").live('click',function(){
		showMedia($(this).attr("hd"),$(this).attr("hd"),$(this).text(),"true");
	});
	var aotoShow = setInterval(function(){
		var dd = $(".vkoteacher").find(".curimg").parent().next();
		if(dd.is(":last")){
			imgClick($(".vkoteacher .fitimg:first"));
		}else{
			imgClick(dd.children(".fitimg"));
		}
	},5000);
	
	//精彩试听
	$(".freecourse .pic").hover(function(){
		$(this).fadeTo("0", 0.8);
	},function(){
		$(this).fadeTo("0", 1);
	});
	$(".sepcial>li").click(function(){
		$(".sepcial>li").removeClass("current");
		$(this).addClass("current");
		var freecourse =$(".freecourse");
		var grade = $(this).attr("grade");
		var url= base+"/loginfreecourse.html";
		$.ajax({
			url:url,
			data:{grade:grade},
			success:function(data){
				data = (new Function("return " + data))();
				freecourse.empty();
				freecourse.append(data);
		}});
	});
	$(".freecourse .pic").live("click",function(){
		showMedia($(this).attr("hd"),$(this).attr("sd"),$(this).next("p").text(),"false",$(".brief",this).html());
	});
	// 新加入的同学
	$(".newfriend a").tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - ", 
		fade: 250, 
		top: 20, 
   		left: 10
	});
	
	//同学们正在...
	var statusArr = new Array();
	$(".studentdo-list").children(".status-item").each(function(index){
		statusArr.push($(this));
		$(this).remove();
	});
	statusArr=getRandomArr(statusArr);
	for(var i=0; i<statusArr.length; i++){
		statusArr[i].clone().appendTo($(".studentdo-list"));
	}
	setInterval(scroll, 5000);
	function scroll(){
		$(".studentdo-list").children(".status-item:last").clone().insertBefore($(".studentdo-list").children(".status-item:first"));
		var ftop=$(".studentdo-list").children(".status-item:first").height()+10;
		$(".studentdo-list").css("top",ftop*-1);
		$(".studentdo-list").animate({top:0},1000);
		$(".studentdo-list").children(".status-item:last").remove();
	}
	function getRandomArr(arr){
		var temp_array = new Array();
		for (var index in arr){
			temp_array.push(arr[index]);
		}
		var return_array = new Array();
		for (var i = 0; i<arr.length; i++){
			if (temp_array.length > 0){
				var arrIndex = Math.floor(Math.random() * temp_array.length);
				return_array[i] = temp_array[arrIndex];
				temp_array.splice(arrIndex, 1);
			}
			else{
				break;
			}
		}
		return return_array;
	}
	$(".ques-tags").each(function(){
		$(this).find("li:last").children("span").remove();
	});
	
	//在微课网能干点啥
	$(".aboutUs").click(function(){
		showMedia($(this).attr("hd"),$(this).attr("hd"),$(this).text(),"true");
	})
	
});